﻿using System;
using System.Data;
using TTSBLL;

namespace TTS
{
    /// <summary>
    ///ProConditionHelper 的摘要说明
    /// </summary>
    public static class ProConditionHelper
    {
        /// <summary>
        /// 检查要提交的所有SN的装配前提条件，分三种情况：
        /// 1.子装配件为叶子节点时，要检验CheckLeafSubSNsPrecondition
        /// 2.子装配件为非叶子节点时，要检验CheckLeafSubSNsPrecondition和CheckSubSNsPrecondition
        /// 3.主装配件，要检验CheckMainSNsPrecondition
        /// </summary>
        /// <returns></returns>
        public static bool CheckLeafSubSNsPrecondition(string sn, string TCode, out string message)
        {
            AssembleItem ai = new AssembleItem();
            if (ai.AssembleItem_SubSNAlreadyExist(sn))
            {
                message = TCode + "的条码" + sn + "作为子装配件装配过，无法再次装配";
                return false;
            }
            message = "";
            return true;
        }
      /*  public static bool CheckLeafSubSNsPrecondition(string sn, out string message)
        {
            AssembleItem ai = new AssembleItem();
            if (ai.AssembleItem_SubSNAlreadyExist(sn))
            {
                message = sn + "作为子装配件装配过，无法再次装配";
                return false;
            }
            message = "";
            return true;
        }*/

        public static bool CheckLeafSubSNsPrecondition(string KCCsn, out string message)
        {
            KCCSNUnion KCCUnion = new KCCSNUnion();
            if (!string.IsNullOrEmpty(KCCUnion.KCCSNUnion_GetbyKCCSN1(KCCsn).KCCSN1))
            {
                message = KCCsn + "已装配过，无法再次装配";
                return false;
            }
            message = "";
            return true;
        }

        public static bool CheckSubSNsPrecondition(string sn, string TCode, out string message)
        {
            Assemble a = new Assemble();
            if (a.Assemble_SNAlreadyExist(sn))
            {
                message = "";
                return true;
            }
            message = TCode + "的条码" + sn + "未作为主装配件装配过，无法装配到主装配件上";
            return false;
        }

        public static bool CheckMainSNsPrecondition(string sn, string TCode, out string message)
        {
            Assemble a = new Assemble();
            if (a.Assemble_SNAlreadyExist(sn))
            {
                message = TCode + "的条码" + sn + "作为主装配件装配过，无法再次装配";
                return false;
            }
            message = "";
            return true;
        }
        public static bool IsPassOrFail(string sn, string procedure, out string message)
        {
            TestStatus a = new TestStatus();

            if (a.GetStatusbySN(sn.Substring(sn.Length - 9, 9), procedure).status == "FAILED")
            {
                message = "该装配件测试不合格，请重新扫描测试合格的装配件！";
                return false;
            }
            if (a.GetStatusbySN(sn.Substring(sn.Length - 9, 9), procedure).status == "RECHECKE")
            {
                message = "该装配件测试结果不是一次完整的测试或者WES软件测试项个数发生了变更！";
                return false;
            }
            if (a.GetStatusbySN(sn.Substring(sn.Length - 9, 9), procedure).status == "QCFAILED")
            {
                message = "该装配件未进行QC检验，请让质量相关人员进行检测！";
                return false;
            }
            if (a.GetStatusbySN(sn.Substring(sn.Length - 9, 9), procedure).status == "QCReCheck")
            {
                message = "该装配件解锁后未进行QC检验，请让质量相关人员重新检测！";
                return false;
            }
            message = "";
            return true;
        }

        public static bool IsCorrect (string productType,string PEC,out string message)
        {
            if(productType=="RRH")
            {
                if(PEC=="KCQR01AA")
                {
                    message = "";
                    return true;
                }
            }
            if (productType == "DM1")
            {
                if (PEC == "KCQR00AB")
                {
                    message = "";
                    return true;
                }
            }
            if (productType == "DM2")
            {
                if (PEC == "KCQR00AC")
                {
                    message = "";
                    return true;
                }
            }
            message = "整机类型所对应的PEC与输入的PEC不一致，请重新确认！";
            return false;
        }

        public static bool IsPackageExsit(string _productPackageStatus, string _kccsn, out string message)
        {
            Package packageBLL = new Package();

            if (_productPackageStatus == "正常品")
            {
                if (!string.IsNullOrEmpty(packageBLL.Package_GetbyKCCSN(_kccsn).KCCSN))
                {                   
                    message = "该整机已包装过，请勿重新录入！";
                    return false;
                }
            }

            if (_productPackageStatus == "客退品")
            {
                if (string.IsNullOrEmpty(packageBLL.Package_GetbyKCCSN(_kccsn).KCCSN))
                {                
                    message = "该整机之前未被包装过，不属于客退品，请确认再录入！";
                    return false;
                }
            }
            message = "";
            return true;
        }
    }



}